let follows = require('../model/follow')
let {
    Router
} = require("express");
let router = new Router();

router.use(require("../middleware/auth.js"));

// 查询关注信息
router.post("/getfollow", async (req, res) => {
    // 获取参数
    const user = req.userInfo._id;
    let { name } = req.body;
    // 执行更新
    const data = await follows.find({
        $and: [{
            user
        },
        {
            name
        },
        ]
    });
    if (data.length <= 0) {
        res.status(200).json({
            msg: "未关注该医生",
            state: false,
            data
        });
        return;
    } else {
        // 响应客户端
        res.status(200).json({
            message: "已关注",
            state: true,
            data
        });
    }
})

// 添加关注
router.post("/addfollow", async (req, res) => {
    const user = req.userInfo._id;
    let { name } = req.body;
    const data = await follows.find({
        $and: [{
            user
        },
        {
            name
        },
        ]
    });
    if (data.length <= 0) {
        await follows.create({ user: user, name: name });
        res.status(200).json({
            msg: "添加关注成功",
        });
        return;
    } else {
        // 响应客户端
        res.status(200).json({
            message: "已关注",
            data
        });
        return;
    }
})

// 移除关注
router.post("/deletefollow", async (req, res) => {
    const user = req.userInfo._id;
    let { name } = req.body;
    const data = await follows.find({
        $and: [{
            user
        },
        {
            name
        },
        ]
    });
    if (data.length <= 0) {
        res.status(200).json({
            msg: "未关注",
        });
        return;
    } else {
        // 响应客户端
        await follows.deleteMany({ user: user, name: name });
        res.status(200).json({
            msg: "移除关注成功",
        });
        return;
    }

})

// 查询关注数量
router.post("/numfollow", async (req, res) => {
    // 获取参数
    const user = req.userInfo._id;
    // 执行更新
    const data = await follows.find({ user: user });
    res.status(200).json({
        data
    });
})



module.exports = router;